﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormProject.Depo.AlisDetay;

namespace WindowsFormProject.Depo
{
    public partial class UrunListesiFrm : Form
    {
        UrunIslemler.UrundetayIslemler UD = new UrunIslemler.UrundetayIslemler();
        public BindingSource MySource = new BindingSource();
        DataSet MyDataSet = new DataSet();
        public Form AcanForm = null;
        Form VeriGonderilecekForm = null;
        public UrunListesiFrm()
        {
            InitializeComponent();
        }
        public UrunListesiFrm(Form Form)
        {
            InitializeComponent();
            VeriGonderilecekForm = Form;
        }
        private void UrunListesiFrm_Load(object sender, EventArgs e)
        {
            GenelIslemler.BackGrounWorkerCalistir(this.backgroundWorker1);
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            MyDataSet.Tables.Add(UD.TumUrunler());
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            MySource.DataSource = MyDataSet;
            MySource.DataMember = MyDataSet.Tables["MErkezUrunler"].TableName;
            GrdUrunList.DataSource = MySource;
           // bindingNavigator1.BindingSource = MySource;
        }

        private void GrdUrunList_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (AcanForm != null)
            {



                if (AcanForm.Name == "UrunFrm")
                {
                    UrunFrm frm = Application.OpenForms["UrunFrm"] as UrunFrm;
                    if (frm != null)
                    {
                        frm.UrunId.Text = GrdUrunList.Rows[e.RowIndex].Cells[0].Value.ToString();
                        frm.KayitGetir(frm.UrunId.Text);
                        this.Close();
                    }
                }
                else if (AcanForm.Name == "SubeUrunFrm")
                {
                    SubeUrunFrm frm = Application.OpenForms["SubeUrunFrm"] as SubeUrunFrm;
                    if (frm != null)
                    {
                        frm.UrunId.Text = GrdUrunList.Rows[e.RowIndex].Cells[0].Value.ToString();
                        frm.SubeUrunIdGetir(Convert.ToInt32(frm.SubeID.SelectedValue), frm.UrunId.Text);
                        this.Close();
                    }
                }
                else if (AcanForm.Name == "UrunTransferFrm")
                {
                    UrunTransferFrm frm = Application.OpenForms["UrunTransferFrm"] as UrunTransferFrm;
                    if (frm != null)
                    {
                        frm.UrunID.Text = GrdUrunList.Rows[e.RowIndex].Cells[0].Value.ToString();
                        frm.Urunisimgetir();
                        this.Close();
                    }
                }




            }
        
        }

        private void GrdUrunList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (GrdUrunList.CurrentRow != null)
            {


                if (VeriGonderilecekForm != null)
                {
                    DataRow Kaynak = ((DataRowView)(MySource.Current)).Row;
                    FrmAlislar Frm = ((FrmAlislar)(VeriGonderilecekForm));
                    int AlisID = 0;
                    if (Frm.DGWAlislar.CurrentRow!=null)
                    {
                        AlisID = Convert.ToInt32(Frm.DGWAlislar[0,Frm.DGWAlislar.CurrentRow.Index ].Value );   
                    }
                    DataTable MyTablo = Frm.MyDataSet.Tables["AlisDetay"];
                    if (MyTablo != null)
                    {
                        DataRow NewRow = MyTablo.NewRow();
                        NewRow["AlisID"] = AlisID ;
                        NewRow["UrunID"] = Kaynak["UrunID"];
                        NewRow["UrunAdi"] = Kaynak["UrunAdi"];
                        NewRow["UrunBirimi"] = Kaynak["UrunBirimi"];
                        NewRow["AlisDetayAdet"] = 0;
                        NewRow["UrunParaCinsi"] = Kaynak["UrunParaCinsi"];
                        NewRow["AlisDetayKDVOrani"] = Kaynak["UrunKDV"];
                        NewRow["AlisDetayIskonto"] = 0;
                        NewRow["AlisDetayFiyat"] = Kaynak["UrunAlisFiyati"];
                        NewRow["AlisDetayAlisKuru"] = 0;
                        NewRow["AlisDetayDovizi"] = 0;
                        #region Malzeme Kontrol Et
                        int Sonuc = AlisDetayProvider.KontrolEtAlisDetayUrun(AlisID, Kaynak["UrunID"].ToString());
                        if (Sonuc<=0)
                        {
                            int durum = AlisDetayProvider.InsertAlisDetay(NewRow);
                            if (durum>0)
                            {
                                 MyTablo.Rows.Add(NewRow);
                                NewRow.AcceptChanges();
                                GenelIslemler.ToolstripMesajYolla(this.Owner.MdiParent, "Malzeme başarıyla eklenmiştir.");
                                Frm.MySourceAlisDetay.Position = Frm.MySourceAlisDetay.Count - 1;
                            }
                            else
                            {
                                GenelIslemler.ToolstripMesajYolla(this.Owner.MdiParent, "Malzeme ekleme işleminde hata oluştu.Ekleme başarısız oldu.");
                            }
                            
                            
                            
                           
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        }
                        else
                        {
                            GenelIslemler.ToolstripMesajYolla(this.Owner.MdiParent, "Eklenmek istenen Urun bu siparişte zaten mevcuttur...!");
                        }
                        
                        
                        
                        
                        
                        //Frm.MySource.Position = Frm.MySource.Count - 1;
                        
                        //this.Close();
                        #endregion
                    }




                }
            }
        }

        private void UrunIdBul_TextChanged(object sender, EventArgs e)
        {
            if ( UrunIdBul.Text.Trim().Length > 0)
            {
                string Filtre = "UrunADi Like '%" + UrunIdBul.Text + "%'";
                MySource.Filter = Filtre;
                MySource.Position = MySource.Count - 1;

            }
            else
            {
                MySource.RemoveFilter();
                MySource.Position = MySource.Count - 1;
            }
        }


    }
}
